package com.sk.photo_album.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.sk.photo_album.Media;
import com.sk.photo_album.MediaInfo;
import com.sk.photo_album.R;
import com.sk.photo_album.databinding.AdapterAlbumBinding;

import java.util.ArrayList;
import java.util.List;

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.Holder> {

    List<MediaInfo> mBeans;

    protected OnRecyclerViewItemClickListener mOnItemClickListener = null;

    private final Boolean showCamera;

    private final int maxSelect;

    /**
     * 选中的媒体文件
     */
    private ArrayList<MediaInfo> mediaCheckList = new ArrayList<>();

    public AlbumAdapter(Boolean showCamera, int maxSelect) {
        this.showCamera = showCamera;
        this.maxSelect = maxSelect;
    }

    public void reloadData(List<MediaInfo> beans) {
        if (mBeans == null) {
            mBeans = new ArrayList<>();
        } else {
            // 先移除数据
            notifyItemRangeRemoved(0, mBeans.size());
            mBeans.clear();
        }

        if (showCamera) {
            // 添加一个默认数据拍照位置
            mBeans.add(new MediaInfo());
        }

        // 再加载数据
        if (beans != null && beans.size() > 0) {
            mBeans.addAll(beans);
        }

        notifyItemRangeInserted(0, getItemCount());
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AdapterAlbumBinding binding = AdapterAlbumBinding.inflate(LayoutInflater.from(parent.getContext()));
        Holder holder = new Holder(binding);
        holder.setOnItemClickListener(mOnItemClickListener);
        holder.setCheckListener(info -> {
            if (info.isChecked()) {
                mediaCheckList.add(info);
                if (mediaCheckList.size() == maxSelect) {
                    notifyDataSetChanged();
                }
            } else {
                mediaCheckList.remove(info);
                if (mediaCheckList.size() == maxSelect - 1) {
                    notifyDataSetChanged();
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.setData(mediaCheckList, maxSelect, mBeans.get(position));
    }

    @Override
    public int getItemCount() {
        return mBeans == null ? 0 : mBeans.size();
    }

    /**
     * 选中媒体文件
     */
    public ArrayList<MediaInfo> getMediaCheckList() {
        return mediaCheckList;
    }

    public MediaInfo getMediaItem(int position) {
        try {
            return mBeans.get(position);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 设置 item 点击事件
     */
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * item 点击事件
     */
    public interface OnRecyclerViewItemClickListener {

        /**
         * item 被点击
         *
         * @param view     被点击的 {@link View}
         * @param position 在 RecyclerView 中的位置
         */
        void onShowImageClick(View view, int position);
    }

    public static class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {

        protected OnRecyclerViewItemClickListener listener = null;

        private OnHolderCheckListener checkListener = null;

        AdapterAlbumBinding binding;

        MediaInfo data;

        public Holder(@NonNull AdapterAlbumBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

            this.binding.getRoot().setOnClickListener(this);
            binding.camera.setOnClickListener(this);

            // 选择
            binding.check.setOnClickListener(v -> {
                data.setChecked(!data.isChecked());
                if (data.isChecked()) {
                    binding.check.setImageResource(R.drawable.ic_checked);
                } else {
                    binding.check.setImageResource(R.drawable.ic_unchecked);
                }
                checkListener.onCheckChange(data);
            });
        }

        /**
         * 设置 item 点击事件
         */
        public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
            this.listener = listener;
        }

        /**
         * @param checkListener listener
         */
        public void setCheckListener(OnHolderCheckListener checkListener) {
            this.checkListener = checkListener;
        }

        public void setData(List<MediaInfo> checkList, int maxSelect, MediaInfo data) {
            if (data.getType() == null) {
                binding.camera.setVisibility(View.VISIBLE);
                return;
            }

            binding.camera.setVisibility(View.GONE);

            this.data = data;

            if (data.getType() == Media.MEDIA_TYPE_VIDEO) {
                binding.playerImage.setVisibility(View.VISIBLE);
                binding.time.setVisibility(View.VISIBLE);
            } else {
                binding.playerImage.setVisibility(View.GONE);
                binding.time.setVisibility(View.GONE);
            }

            if (data.isChecked()) {
                binding.check.setImageResource(R.drawable.ic_checked);
            } else {
                if (checkList.size() >= maxSelect) {
                    binding.check.setEnabled(false);
                    binding.check.setImageResource(R.drawable.ic_check_enable);
                } else {
                    binding.check.setEnabled(true);
                    binding.check.setImageResource(R.drawable.ic_unchecked);
                }
            }

            Glide.with(binding.getRoot().getContext())
                    .load(data.getPath())
                    .into(binding.image);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                int position = getLayoutPosition();
                if (position != RecyclerView.NO_POSITION) {
                    listener.onShowImageClick(v, getLayoutPosition());
                }
            }
        }

        /**
         * item 点击事件
         */
        public interface OnHolderCheckListener {

            /**
             * item 被点击
             *
             * @param info MediaInfo
             */
            void onCheckChange(MediaInfo info);
        }
    }
}
